package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TCabinet;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TCabinetService;
import com.ruipengkj.view.base.BaseDialog;

public class MemberCabinetAdd extends BaseDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JLabelJTextFieldComponent name= null;//商柜名称
	private JLabelJTextAreaComponent des = null;//商柜描述
	private JLabelJComboBoxComponent type = null; //商柜类型
	public boolean isSuccess=false;  //判断是否成功
	private TCabinetService tCabinetService=BuildSpringApplication.getInstance().getTCabinetServiceImpl();
	
	public MemberCabinetAdd(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
		//this.getElements();
	}
	private void initListener(){
		okButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		TCabinet tCabinet = new TCabinet();
		tCabinet.setCabinetID(UUID.GenUUID15());
		
		
		if(name.getText().getText().trim().equals("")){
			JOptionPane.showMessageDialog(null, "商柜名称不能为空！");
			name.getText().requestFocus();
			return;
		}else if(des.getArea().getText().trim().equals("")){
			JOptionPane.showMessageDialog(null, "商柜描述不能为空！");
			des.getArea().requestFocus();
			return;
		}else if(type.getBox().getSelectedIndex() == 0){
			JOptionPane.showMessageDialog(null, "请选择类型！");
			return;
		}else{
			tCabinet.setName(name.getText().getText().trim());
			tCabinet.setDes(des.getArea().getText().trim());
			tCabinet.setStyle((byte)type.getBox().getSelectedIndex());
			if(e.getSource()==okButton){
				//确定
				try {
					tCabinetService.saveOrUpdateService(tCabinet);
					isSuccess=true;
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					isSuccess=false;
					e1.printStackTrace();
				}

				this.dispose();
			}
		}
	}
	private void initViews() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		name=new JLabelJTextFieldComponent("商柜编号：",30);
		des = new JLabelJTextAreaComponent("商柜描述：", 30,5);
		type=new JLabelJComboBoxComponent("充值类型：",new String[]{},188);
		type.addElement(new JComboBoxPOJO("请选择……"));
		type.addElement(new JComboBoxPOJO("普通列表显示"));
		JPanel jp1=new JPanel();
		okButton=new JButton("确定");
		jp1.add(okButton);
		
		main_jp.add(name);
		main_jp.add(des);
		main_jp.add(type);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	public void showDialog(){
		this.setVisible(true);
	}
	public void showDialog(TCabinet tCabinet){
		this.setVisible(true);
	}
}
